package j20251024;

public class BlockingQueue {
    private String[] data=null;//循环队列
    private int head=0;
    private int tail=0;
    private int size=0;

    public BlockingQueue(int capacity) {
        this.data=new String[capacity];
    }

    public void put(int elem) throws InterruptedException {
        synchronized (this){
            while (size>= data.length){
                this.wait();//阻塞
            }
            data[tail++]=String.valueOf(elem);
            if(tail>=data.length){
                tail=0;
            }
            size++;
            this.notify();
        }
    }

    public String take() throws InterruptedException {
        synchronized (this){
            while (size==0){
                this.wait();
            }
            String result=data[head++];
            if(head>=data.length){
                head=0;
            }
            size--;
            this.notify();
            return result;
        }
    }
}
